﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Interface;
using PDAMain.Entity;
using System.Text.RegularExpressions;

namespace PDAMain.Forms.RepairWork
{
    public partial class RepairAsset : Form
    {
        public RepairAsset()
        {
            InitializeComponent();
        }

        private System.Windows.Forms.Label lbldescripton;
        private System.Windows.Forms.TextBox txtnum;
        private System.Windows.Forms.Label lblissue;
        int[] value = null;
        DataTable dt = null;
        private void RepairAsset_Load(object sender, EventArgs e)
        {
            if (NetWorkState.checkWifiState() || NetWorkState.checkGprsState())
            {
                dt = DataHelper.RepairWorkDao.GetAssetDetail(DataHelper.RepairWorkDao.AssetNum);
                //DataTable dt = CreateTable();
                if (dt.Rows.Count > 0)
                {
                    AddControl(dt);
                }
            }
        }

        private void AddControl(DataTable dt)
        {
            value = new int[dt.Rows.Count];
            int y = 0;
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                if (index == 0)
                {
                    y = 18;
                }
                else
                {
                    y += 33;
                }
                string descripton = "description_" + index.ToString();
                string num = "num_" + index.ToString();
                string issue = "issue_" + index.ToString();
                lbldescripton = new System.Windows.Forms.Label();
                txtnum = new System.Windows.Forms.TextBox();
                lblissue = new System.Windows.Forms.Label();

                this.lbldescripton.Location = new System.Drawing.Point(17, y);
                this.lbldescripton.Name = descripton;
                this.lbldescripton.Size = new System.Drawing.Size(100, 20);
                this.lbldescripton.Text = dt.Rows[index]["DESCRIPTION"].ToString();
                this.lbldescripton.TextAlign = System.Drawing.ContentAlignment.TopRight;
                this.Controls.Add(lbldescripton);

                this.txtnum.Location = new System.Drawing.Point(129, y);
                this.txtnum.Name = num;
                this.txtnum.Size = new System.Drawing.Size(80, 20);
                this.txtnum.TabIndex = 1;
                this.txtnum.Text = "0";
                this.txtnum.ForeColor = System.Drawing.Color.Gray;
                this.txtnum.GotFocus += new System.EventHandler(this.txtnum_GotFocus);
                this.Controls.Add(txtnum);

                value[index] = this.Controls.Count - 1;

                this.lblissue.Location = new System.Drawing.Point(221, y);
                this.lblissue.Name = issue;
                this.lblissue.Size = new System.Drawing.Size(43, 20);
                this.lblissue.Text = dt.Rows[index]["ISSUEUNIT"].ToString();

                this.Controls.Add(lblissue);
            }
        }

        private void txtnum_GotFocus(object sender, EventArgs e)
        {
            for (int index = 0; index < value.Length; index++)
            {
                if (this.Controls[value[index]].Focused)
                {
                    TextBox txtInputNum = this.Controls[value[index]] as TextBox;
                    txtInputNum.Text = "";
                    this.txtnum.ForeColor = System.Drawing.Color.Black;
                }
            }
        }

        //检查输入值为有效数字
        private bool VerifyNum()
        {
            bool result = false;
            string s = "^[1-9][0-9]*$";//正则表达式
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                TextBox txtInputNum = this.Controls[value[index]] as TextBox;
                if (txtInputNum.Text.Trim() != "0")
                {
                    Regex re = new Regex(s);
                    if (!re.IsMatch(txtInputNum.Text.Trim()))
                    {
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (VerifyNum())
            {
                MessageBox.Show("请输入正确的数字");
                return;
            }
            else
            {
                int error = 0;
                Identity.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string assignmentId = DataHelper.RepairWorkDao.AssignmentId;
                string wonum = Identity.wonum;
                string barcode = DataHelper.RepairWorkDao.AssetNum;
                string person = Identity.dutyPerson;
                string detail = string.Empty;
                for (int index = 0; index < dt.Rows.Count; index++)
                {
                    TextBox txtInputNum = this.Controls[value[index]] as TextBox;
                    if (txtInputNum.Text.Trim() != "" && txtInputNum.Text.Trim() != "0")
                    {
                        string itemnum = dt.Rows[index]["itemNum"].ToString().Trim();
                        string quantity = txtInputNum.Text.Trim();
                        if (index == dt.Rows.Count - 1)
                        {
                            detail += itemnum + "," + quantity;
                        }
                        else
                        {
                            detail += itemnum + "," + quantity + ";";
                        }
                    }
                }
                bool result = DataHelper.RepairWorkDao.insertMaterial(assignmentId, wonum, barcode, person, detail);
                //bool result = false;
                if (result)
                {
                    MessageBox.Show("成功保存到远程数据库");
                }
                else  //上次webservice失败
                {
                    bool insert = DataHelper.RepairWorkDao.InsertRepairAsset(wonum, barcode, detail, person, assignmentId);
                    //bool insert = true;
                    if (!insert)
                    {
                        error++;
                    }
                    if (error > 0)
                    {
                        MessageBox.Show("保存失败" + error + "条数据");
                    }
                    else
                    {
                        MessageBox.Show("同步数据失败，保存至本地数据库");
                    }
                }
                DataHelper.RepairWorkDao.AssignmentId = "";
                RepairWorkList list = new RepairWorkList();
                list.Show();
                this.Close();
            }
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            RepairWorkDetail pw = new RepairWorkDetail();
            pw.Show();
            this.Close();
        }
    }
}